clear; close all; clc; 

load ../data/music_dataset.mat
addpath 'CV/' 'DT/' 'Lyrics_Kernel/' 'SVM/libsvm/'

[Xt_lyrics] = make_lyrics_sparse(train, vocab);
[Xq_lyrics] = make_lyrics_sparse(quiz, vocab);

Yt = zeros(numel(train), 1);
for i=1:numel(train)
    Yt(i) = genre_class(train(i).genre);
end

Xt_audio = make_audio(train);
Xq_audio = make_audio(quiz);

%% Split data into 60% Train and 40% Test

n = size(Yt, 1);
n_folds = 5;

%choose the number of partitions
part = make_cv_partition(n, n_folds);

cv_ind = find(part~= 4 & part~= 5);
test_ind = find(part==4 | part == 5);

%Index the X and Y arrays by the appropriate indicies

cv_audio = Xt_audio(cv_ind,:); %Training set (80%)
cv_labels = Yt(cv_ind,:); %Training labels (80 % )
test_audio = Xt_audio(test_ind,:); %Test set (20 %)
test_labels = Yt(test_ind,:); % Test labels (20 % )

%% Confusion Matrix for DT

% train 
dt = dt_train_multi(cv_audio, cv_labels, 6); 
% test
scores = dt_test_multi(dt, test_audio);
% best guess class
[~, test_pred] = max(scores, [], 2); 
% CONFUSION!>!?!?@RE$@USA!~!!!!!
Mtree = confusion_matrix(test_pred, test_labels); 

%% Confusion Matrix for SVM

Xt_lyrics = (Xt_lyrics - repmat(min(Xt_lyrics,[],1),size(Xt_lyrics,1),1))* ...
    spdiags(1./(max(Xt_lyrics,[],1)-min(Xt_lyrics,[],1))',0,size(Xt_lyrics,2),size(Xt_lyrics,2));


kernel_cv = kernel_intersection(Xt_lyrics(cv_ind,:), Xt_lyrics(cv_ind,:));
kernel_test = kernel_intersection(Xt_lyrics(cv_ind,:), Xt_lyrics(test_ind,:)); 

train_struct = svmtrain(Yt(cv_ind), [(1:size(kernel_cv,1))' kernel_cv],...
    sprintf('-t 4 -b 1 -c %g', 1));

randomshit = repmat((1:10)',floor(size(test_ind,2)/10),1);
randomshit = [randomshit; ones(size(test_ind,2)-10*floor(size(test_ind,2)/10),1)];

[~, ~, probabilities] = svmpredict(randomshit, ...
    [(1:size(kernel_test,1))' kernel_test], train_struct, '-b 1');

ordered_prob = zeros(size(probabilities)); 
ordered_prob(:,train_struct.Label) = probabilities; 

[~, test_pred] = max(ordered_prob, [], 2); 
% CONFUSION!>!?!?@RE$@USA!~!!!!!
MSVM = confusion_matrix(test_pred, Yt(test_ind));

%% Confusion Matrix for NB

nb = NaiveBayes.fit(Xt_audio(cv_ind,:), Yt(cv_ind),'Distribution','kernel');
nb_scores = posterior(nb, Xt_audio(test_ind,:));
[~, test_pred] = max(nb_scores, [], 2); 
% CONFUSION!>!?!?@RE$@USA!~!!!!!
MNB = confusion_matrix(test_pred, Yt(test_ind));


